package com.example.filesaveqq;

import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FileSaveQQ {
    //保存QQ账号和密码到data.txt文件中
    public static boolean saveUserInfo(Context context, String account, String password) {
        FileOutputStream fos = null;
        try {
            //获取文件的输出流对象
            fos = context.openFileOutput("data.txt", Context.MODE_PRIVATE);
            fos.write((account + ":" + password).getBytes());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //从data.txt文件中获取存储的QQ账号和密码
    public static Map<String, String> getUserInfo(Context context) {
        String content = "";
        FileInputStream fis = null;
        try {
            fis = context.openFileInput("data.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            content = new String(buffer);
            Map<String, String> userMap = new HashMap<String, String>();
            String[] infos = content.split(":");
            userMap.put("account", infos[0]);
            userMap.put("password", infos[1]);
            return userMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static Bundle getUserInfo1(Context context){
        String content="";
        Bundle bundle=new Bundle();
        FileInputStream fis=null;
        try {
            fis=context.openFileInput("data.txt");
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            content=new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis!=null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String[] infos=content.split(":");
        bundle.putString("account",infos[0]);
        bundle.putString("password",infos[1]);
        return bundle;
    }

    public static ContentValues getUserinfo2(Context context){
        String content="";
        ContentValues contentValues=new ContentValues();
        FileInputStream fis=null;
        try {
            fis=context.openFileInput("data.txt");
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            content=new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis!=null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String[] infos=content.split(":");
        contentValues.put("account",infos[0]);
        contentValues.put("password",infos[1]);
        return contentValues;
    }
}
